﻿using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class main : Page
{
    private String questURL = "http://www.sipo.gov.cn/sipo2008/zcfg/zcjd/";//"http://www.cnlaw.org.cn/soft/kstd/1.html";
    private String newsURL = "http://www.sipo.gov.cn/sipo2008/yw/";
    private String policyURL = "http://www.sipo.gov.cn/sipo2008/zcfg/zcjd/";
    private String softURL = "http://www.cnlaw.org.cn/soft/kstd/";
    private const int LIMITED_LENGTH = 22;
    private const int SUB_STRAT = 7;
    private const int SUB_LENGTH = 9;

    protected void Page_Load(object sender, EventArgs e)
    {
        /*WebRequest request = WebRequest.Create(questURL);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
        String result = reader.ReadToEnd();
        //TestLabel.Text = result;
        Regex reg = new Regex("<ul(.|\n)*?</ul>"); //new Regex("<table.*(?=table17)(.|\n)*?</table>");
        //Regex reg2 = new Regex("<tr.*(.|\n)*?</tr>");
        Match match = reg.Match(result);
        String str = match.Value.Replace("<ul>", "<ul style=\"border: 0px solid gray; width: 150px;margin-left:-15px;font-size:12px\">");
        createBulletList(this.policyBulletList, str);

        request = WebRequest.Create(newsURL);
        response = request.GetResponse();
        reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
        result = reader.ReadToEnd();
        reg = new Regex("<ul(.|\n)*?</ul>");
        match = reg.Match(result);
        str=match.Value.Replace("<ul>","<ul style=\"border: 0px solid gray; width: 150px;margin-left:-15px;font-size:12px\">");
        createBulletList(this.newsBulletList,str);
        reader.Close();
        reader.Dispose();
        response.Close();
        //SiteMapPath.Visible = false;*/
    }

    private void createBulletList(BulletedList bulletList,String str)
    {
        try
        {
            if (bulletList.Items.Count != 0)
            {
                bulletList.Items.Clear();
            }
            Regex reg1 = new Regex("<.+?>", RegexOptions.IgnoreCase);
            Regex reg = new Regex("<a(.|\n)*?</a>");
            Regex HrefReg = new Regex(@"href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*");
            IEnumerator enumers = reg.Matches(str).GetEnumerator();
            int i = 0;
            while (enumers.MoveNext() && enumers.Current != null&&i<10)
            {
                Label button = new Label();
                button.Style.Add("margin-left", "3px");
                String value = HrefReg.Match(((Match)enumers.Current).Value).Value;
                String text = reg1.Replace((((Match)enumers.Current).Value), "").Replace("<", "").Replace(">", "").Replace("&nbsp;", "");
                bulletList.DisplayMode = BulletedListDisplayMode.LinkButton;
                if (text.Length > LIMITED_LENGTH)
                {
                    text = text.Substring(0, LIMITED_LENGTH-1) + "...";
                }
                ListItem item = new ListItem(text, value.Substring(SUB_STRAT, value.IndexOf("target") - SUB_LENGTH));
                bulletList.Items.Add(item);
                i++;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    protected void NewBulletedListClick(object sender, BulletedListEventArgs e)
    {
        viewBulletDetail(newsURL, ((BulletedList)sender).Items[e.Index], "<div.*(?=q_wz)(.|\n)*?</div>");
    }

    protected void PolicyBulletedListClick(object sender, BulletedListEventArgs e)
    {
        viewBulletDetail(policyURL, ((BulletedList)sender).Items[e.Index], "<div.*(?=q_wz)(.|\n)*?</div>");
    }

    private void viewBulletDetail(String url,ListItem item,String exp)
    {
        WebRequest request = WebRequest.Create(url + item.Value);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
        String result = reader.ReadToEnd();
        Regex reg = new Regex(exp);
        //this.content.Controls.Add(this.ParseControl(reg.Match(result).Value));
        //this.content.InnerHtml = "<h2 style=\"text-align:center\">" + item.Text + "</h2>" + reg.Match(result).Value.Replace("盛邦", "天玺泽").Replace("align=\"center\"", "align=\"left\"") + "</div>";
        //.Replace("div id=\"bottom\"", "div id=\"bottom\" style=\"display:none\"").Replace("div id=\"header\"", "div id=\"header\" style=\"display:none\"");
    }

    protected void ViewSoftDetails(object sender, BulletedListEventArgs e)
    {
        String value = ((BulletedList) sender).Items[e.Index].Value;
        String[] seperator=new string[]{";"};
        String[] result=value.Split(seperator, System.StringSplitOptions.None);
        Boolean isRef = Boolean.Parse(result[0]);
        if (isRef)
        {
            viewBulletDetail(softURL, ((BulletedList) sender).Items[e.Index], "<div.*(?=tart)(.|\n)*?</div>");
        }else
        {
            Response.Redirect(result[1]);
        }
    }
}